/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package RRHH_controlador;

import Conexion.Conexion;
import RRHH_Modelo.RHH_DEPARTAMENTO;
import RRHH_Modelo.RHH_REGIONAL;
import RRHH_Modelo.RHH_REGIONAL_DEPTO;
import RRHH_Modelo.RHH_USUARIO;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author equipo_4
 */
@WebServlet(name = "RRHH_DEPARTAMENTOS_CONTROLLER", urlPatterns = {"/RRHH_DEPARTAMENTOS_CONTROLLER"})
public class RRHH_DEPARTAMENTOS_CONTROLLER extends HttpServlet {
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String proceso=request.getParameter("proceso");
            RHH_USUARIO usuario = ((RHH_USUARIO)request.getSession().getAttribute("usr"));
            Conexion con = new Conexion(usuario.getUSUARIO(), usuario.getPASSWORD());
            String html="";
            switch(proceso){
                case "GuardarNuevo":
                    html=GuardarNuevo(request,con);
                    break;
                case "seleccionarSubMenu":
                    html=seleccionarSubMenu(con,request);
                    break;
                    
                case "quitarAgregarRoll":
                    html=quitarAgregarRoll(con,request,usuario);
                    break;
                case "Eliminar":
                    html=eliminarPerfil(con,request);
                    break;
            }
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain");
            response.getWriter().write(html);
            con.Close();
        } catch (SQLException ex) {
            Logger.getLogger(RRHH_DEPARTAMENTOS_CONTROLLER.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }
    public String eliminarPerfil(Conexion con, HttpServletRequest request) throws SQLException{
        int id = Integer.parseInt(request.getParameter("Eliminar"));
        RHH_DEPARTAMENTO depto = new RHH_DEPARTAMENTO(con);
        depto.setCon(con);
        depto = depto.BuscarXid(id);
        depto.EliminarXid();
        return actualizarPerfil(con);
    }
    
    public String quitarAgregarRoll(Conexion con, HttpServletRequest request,RHH_USUARIO usuario ) throws SQLException{
        double iddepto = Double.parseDouble(request.getParameter("IdSubMenu"));
        double idregional = Double.parseDouble(request.getParameter("idRegional"));
        RHH_REGIONAL_DEPTO depto_regional = new RHH_REGIONAL_DEPTO();
        depto_regional.setCon(con);
        try
        {
            depto_regional=depto_regional.BuscarXid(idregional, iddepto);
            depto_regional.Desabilitar(iddepto, idregional);
        }
        catch(Exception ex)
        {
            depto_regional = new RHH_REGIONAL_DEPTO();
            depto_regional.setCon(con);
            depto_regional.Abilitar(iddepto, idregional);
        }
        return seleccionarSubMenu(con, request);
    }
    
    
    
    public String seleccionarSubMenu(Conexion con, HttpServletRequest request) throws SQLException{
        Double IdSubMenu = Double.parseDouble(request.getParameter("IdSubMenu"));
        RHH_DEPARTAMENTO depto = new RHH_DEPARTAMENTO(con);
        depto.setCon(con);
        depto = depto.BuscarXid(IdSubMenu);
        
        RHH_REGIONAL regional = new RHH_REGIONAL(con);
        regional.setCon(con);
        List<RHH_REGIONAL> lista = regional.Todos();
        
        RHH_REGIONAL_DEPTO regionaldepto = new RHH_REGIONAL_DEPTO();
        regionaldepto.setCon(con);
        
        List<RHH_REGIONAL_DEPTO> ListaActivos = regionaldepto.TodosLosActivos(IdSubMenu);
        List<RHH_REGIONAL> ListaInactivos = regional.TodosLosInactivos(IdSubMenu);
        
        String html="<div class='contenedor50 c1' style=\"border-right: solid white thin;\" data-estado='inactivo'>";
        for(int i = 0; i<ListaInactivos.size(); i++)
        {
            html+="<div class='draggable ui-draggable ui-draggable-handle ui-droppable' data-estado='inactivo' data-idRegional="+ListaInactivos.get(i).getID()+">"
                    + ListaInactivos.get(i).getDESCRIPCION()
                    +"</div>";
        }
        html+="</div><div class='contenedor50 c2' data-estado='activo'>"
                ;
        for(int i = 0; i<ListaActivos.size(); i++)
        {
            regional = regional.BuscarXid(ListaActivos.get(i).getID_REGIONAL());
            html+="<div class='draggable ui-draggable ui-draggable-handle ui-droppable' data-estado='activo' data-idRegional="+ListaActivos.get(i).getID_REGIONAL()+">"
                    + regional.getDESCRIPCION()
                    +"</div>";
        }
        html+="</div>";
        return html;
    }
    public String actualizarPerfil(Conexion con) throws SQLException{
        RHH_DEPARTAMENTO depto = new RHH_DEPARTAMENTO(con);
        depto.setCon(con);
        List<RHH_DEPARTAMENTO> Lista = depto.Todos();
        String html="";
        for(int i =0; i<Lista.size(); i++)
        {
            html+="<span  class=\"subMenu\" data-value=\'"+Lista.get(i).getID()+"\'>"+Lista.get(i).getDESCRIPCION()+"  <span class='btnEliminar' onclick=\"eliminarPerfil("+Lista.get(i).getID()+")\">X</span></span><br/>";
        }
        return html;
    }
    public String GuardarNuevo(HttpServletRequest request, Conexion con) throws SQLException{
        String desc = request.getParameter("descripcion");
        RHH_DEPARTAMENTO depto = new RHH_DEPARTAMENTO(0, desc);
        depto.setCon(con);
        depto.Insertar();
        return actualizarPerfil(con);
    }
    
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
